<!--
* @author: lzm
* @date: 2023年11月27日10:52:10
* @description：库存管理-库存余量预警
* @update: 2023年11月27日10:52:16
-->
<template>
  <div class="box">
    <div class="inventory-top form-top">
      <el-form ref="balanceForm" :model="balanceForm" label-width="110px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="物料名称">
              <el-select
                filterable
                style="width: 100%"
                v-model="balanceForm.name"
                placeholder="请选择"
              >
                <el-option
                  v-for="(item, index) in materialName"
                  :key="index"
                  :label="item"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格型号">
              <el-select
                filterable
                style="width: 100%"
                v-model="balanceForm.model"
                placeholder="请选择"
              >
                <el-option
                  v-for="(item, index) in standardsList"
                  :key="index"
                  :label="item"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="关键字">
              <el-input
                style="width: 100%"
                v-model.trim="balanceForm.keyword"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" style="text-align: center">
            <el-button class="chamfer-98" type="primary" @click="searchBtn">
              查询
            </el-button>
            <el-button
              style="margin-left: 20px"
              class="chamfer-98"
              plain
              @click="resetBtn"
            >
              重置
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="inventory-bot table-normal">
      <div class="btnBox" style="text-align: right">
        <el-button type="primary" class="chamfer-98" @click="exportBtn">
          <span class="btn-middle">
            <span>导出</span>
            <img class="btn-icon" src="~@/assets/material_img/exportBtn.png" />
          </span>
        </el-button>
      </div>
      <el-table
        v-loading="loading"
        element-loading-background="rgba(0, 0, 0, 0.6)"
        :data="balanceTable"
        :row-key="getRowKeys"
        :expand-row-keys="expands"
        ref="refTable"
        fit
        style="width: 100%"
        header-row-class-name="tableHeader"
        header-cell-class-name="tableTrHeader"
        row-class-name="tableRow"
        cell-class-name="tableCell"
        @row-click="clickTable"
        @expand-change="expandSelect"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          align="center"
          type="selection"
          width="80"
          label-class-name="DisabledSelection"
        />
        <el-table-column type="index" label="序号" width="80" align="center" />
        <el-table-column
          prop="name"
          label="物料名称"
          width="200"
          :show-overflow-tooltip="true"
          align="center"
        />
        <el-table-column prop="model" label="规格型号" align="center" />
        <el-table-column
          prop="totalCount"
          label="在库数量"
          align="center"
          width="140"
        />
        <el-table-column prop="warnCount" label="预警数量" align="center" />
        <el-table-column label="状态" align="center">
          <template v-slot="{ row }">
            <span :class="{ redFont: row.totalCount <= row.warnCount }">{{
              row.totalCount > row.warnCount ? '正常' : '缺料'
            }}</span>
          </template>
        </el-table-column>
        <el-table-column type="expand" width="1">
          <template v-slot="{ row }">
            <el-table
              :data="row.secondTable"
              border
              header-row-class-name="tableHeaderSnd"
              header-cell-class-name="tableTrHeaderSnd"
              row-class-name="tableRowSnd"
              cell-class-name="tableCellSnd"
              style="width: 98%; margin: 0 auto"
            >
              <el-table-column
                prop="shelvesName"
                label="货架名称"
                align="center"
              >
              </el-table-column>
              <el-table-column
                prop="positionName"
                label="库位号"
                align="center"
              >
                <template v-slot="{ row }">
                  {{row.layerSort}}-{{row.positionName}}
                </template>
              </el-table-column>
              <el-table-column prop="stock" label="在库数量" align="center">
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <paging-component
        :total="total"
        :page-size="balanceForm.currentSize"
        :current-page="balanceForm.currentPage"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import {
  getWarnRecordList,
  getGoodsPositionBindApi
} from '@/api/inventoryManage'
import {
  getMaterialNameList,
  getMaterialModelList,
  exportExportList, exportExportBindList
} from '@/api/materialManage'
import pagingComponent from '@/components/PagingComponent/index.vue'
export default {
  components: { pagingComponent },
  data() {
    return {
      // 库存余量表单
      balanceForm: {
        name: '', // 物料名称
        model: '', // 规格型号
        keyword: '', // 关键字
        currentPage: 1, // 页码
        currentSize: 20 // 页尺寸
      },
      total: 0, // 库存余量总数
      // 物料名称
      materialName: [],
      // 规格型号
      standardsList: [],
      // 库存余量预警记录列表
      balanceTable: [],
      expands: [], // 展开行,数组形式
      multipleSelection: [], // 表格选中数据
      loading: false
    }
  },
  created() {
    this.renderList()
  },
  methods: {
    // 获取列表
    async renderList() {
      this.loading = true
      const { result } = await getWarnRecordList(this.balanceForm)
      this.total = Number(result.totalCount)
      this.balanceTable = result.currentData
      // 更新下拉菜单数据
      let { result: list1 } = await getMaterialNameList()
      this.materialName = list1
      let { result: list2 } = await getMaterialModelList()
      this.standardsList = list2
      this.expands = []
      this.loading = false
    },
    // 查询
    searchBtn() {
      this.balanceForm.currentPage = 1
      this.renderList()
    },
    // 重置
    resetBtn() {
      this.balanceForm = {
        name: '', // 物料名称
        model: '', // 规格型号
        keyword: '', // 关键字
        currentPage: 1, // 页码
        currentSize: 20 // 页尺寸
      }
      this.renderList()
    },
    // pageSize改变时会触发(每页数据条数)
    handleSizeChange(val) {
      this.balanceForm.currentSize = val
      this.renderList()
    },
    // currentPage改变时会触发(当前页码数)
    handleCurrentChange(val) {
      this.balanceForm.currentPage = val
      this.renderList()
    },
    // 导出
    async exportBtn() {
      if (this.multipleSelection.length > 0) {
        // 创建一个数组，里面存勾选数据的id
        let delArrayData = []
        this.multipleSelection.forEach((item) => {
          delArrayData.push(item.id)
        })
        await exportExportBindList({ ids: delArrayData })
          .then((result) => {
            const content = result
            const blob = new Blob([content], {
              type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            }) //处理返回
            const fileName = '库存信息.xlsx'
            if ('download' in document.createElement('a')) {
              // 非IE下载
              const elink = document.createElement('a')
              elink.download = fileName
              elink.style.display = 'none'
              elink.href = URL.createObjectURL(blob)
              document.body.appendChild(elink)
              elink.click()
              URL.revokeObjectURL(elink.href) // 释放URL 对象
              document.body.removeChild(elink)
            } else {
              // IE10+下载
              navigator.msSaveBlob(blob, fileName)
            }
          })
          .catch((err) => {
            this.$message({
              type: 'error',
              message: '导出失败!'
            })
          })
      } else {
        await exportExportBindList(this.balanceForm)
          .then((result) => {
            const content = result
            const blob = new Blob([content], {
              type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            }) //处理返回
            const fileName = '库存信息.xlsx'
            if ('download' in document.createElement('a')) {
              // 非IE下载
              const elink = document.createElement('a')
              elink.download = fileName
              elink.style.display = 'none'
              elink.href = URL.createObjectURL(blob)
              document.body.appendChild(elink)
              elink.click()
              URL.revokeObjectURL(elink.href) // 释放URL 对象
              document.body.removeChild(elink)
            } else {
              // IE10+下载
              navigator.msSaveBlob(blob, fileName)
            }
          })
          .catch((err) => {
            this.$message({
              type: 'error',
              message: '导出失败!'
            })
          })
      }
    },
    // 获取勾选数据表格
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 点击表格行
    async clickTable(row, index, e) {
      this.$refs.refTable.toggleRowExpansion(row)
      if (!row.secondTable) {
        let i = this.balanceTable.findIndex((ele) => ele.id == row.id)
        let { result } = await getGoodsPositionBindApi({ goodsId: row.id })
        this.$set(this.balanceTable[i], 'secondTable', result)
      }
    },
    // 每行的唯一key值 用其id表示
    getRowKeys(row) {
      return row.id
    },
    // 展开行同时折叠其他行
    expandSelect(row, expandedRows) {
      let that = this
      if (expandedRows.length) {
        that.expands = []
        if (row) {
          that.expands.push(row.id)
        }
      } else {
        that.expands = []
      }
    }
  }
}
</script>
<style lang="scss">
@import '~@/styles/_handle.scss';
.inventory-bot {
  .el-table {
    // 表格全选框去除空框
    .DisabledSelection .cell .el-checkbox__inner {
      display: none;
      position: relative;
    }
    .el-table__body-wrapper{
      height: calc(100% - 48px);
      overflow-y: auto;
    }
    .el-table__body-wrapper::-webkit-scrollbar{
      width: 6px;
      height: 8px;
    }
    // 二级
    .tableTrHeaderSnd {
      font-weight: normal;
      border: 1px solid;
      @include border_color('second_table_border_color');
      @include background('second_table_head_bg_color');
    }
    // 二级
    .tableCellSnd {
      color: #ffffff;
      font-size: 18px;
      border: 1px solid;
      @include border_color('second_table_border_color');
      @include background('second_table_row_bg_color');
    }
    .expanded {
      @include background('second_table_expanded_bg_color');
    }
  }
}
.el-table--enable-row-hover .el-table__body tr:hover > td.el-table__cell {
  @include background('table_hover_color');
}
</style>
<style scoped lang="scss">
@import '~@/styles/_handle.scss';
.box{
  height: 100%;
}
.inventory-top {
  @include form_bgcolor('form_bgcolor');
}
.inventory-bot{
  height: calc(100% - 144px);
}
::v-deep.inventory-top {
  padding: 20px;
  width: 100%;
  height: 142px;
  border-radius: 10px;
  .el-form-item {
    margin-bottom: 16px;
  }
  .chamfer-98 {
    font-size: 18px;
  }
}
::v-deep .inventory-bot {
  padding: 20px 10px;
  .btnBox {
    height: 44px;
    .chamfer-98 {
      font-size: 18px;
      text-align: center;
      .btn-middle {
        display: flex;
        align-items: center;
        .btn-icon {
          margin-left: 4px;
          width: 16px;
          height: 16px;
        }
      }
    }
  }
  .el-table {
    height: calc(100% - 120px);
    tr {
      background: transparent;
    }
    margin-top: 14px;
    background: transparent;
    border: none;
    .tableTrHeader {
      border: none;
    }
    // 二级
    .tableHeaderSnd {
      color: #ffffff;
      font-size: 18px;
    }
    // 隐藏折叠小箭头
    .el-table__expand-icon {
      color: transparent;
    }
    .el-table::before {
      left: 0;
      bottom: 0;
      width: 100%;
      height: 0;
    }
  }
}

// 二级表格行背景色透明
::v-deep .el-table__cell.el-table__expanded-cell {
  background: transparent;
}
// 暂无数据时展示样式
::v-deep .el-table__empty-block {
  min-height: 240px;
  font-size: 20px;
}
// 取消表格底部横线
::v-deep .el-table--border::after,
.el-table--group::after,
.el-table::before {
  content: none;
}
// 缺料红色
.redFont {
  font-size: 18px;
  color: #ff1f06;
  font-weight: 700;
}
</style>
